package cn.fl.capital.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.comm.facade.framework.constant.SysDictionaryConstant;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.fl.insurance.constant.InsuranceConstant;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.service.IPrjLnkCompBkAccService;
import cn.fl.project.service.IPrjLnkCompService;
import cn.fl.project.service.IPrjPrdDtlService;
import cn.fl.project.vo.PrjLnkCompBkAccVO;
import cn.fl.project.vo.PrjPrdDtlVO;
import fl.constant.facade.framework.constant.FlowConstant;
import fl.spl.facade.framework.enums.CommonAudStsCdEnum;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompBkAccQC;
import fl.spl.facade.rebacc.api.ISaleRebAccInfoApiService;
import fl.spl.facade.rebacc.condition.SaleRebAccInfoQC;
import fl.spl.facade.rebacc.dto.SaleRebAccInfoDTO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapPayeeInfoQueryCondition;
import cn.fl.capital.constant.CapPayInfoConstant;
import cn.fl.capital.constant.CapPayeeInfoConstant;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.dto.CapPayeeInfoDTO;
import cn.fl.capital.service.ICapPayeeInfoApiService;
import cn.fl.capital.service.ICapPayeeInfoService;
import cn.fl.capital.transform.CapPayeeInfoVOTransform;
import cn.fl.capital.vo.CapPayeeInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.dto.PrjPrdPaySchDDTO;
import cn.fl.project.service.IPrjPrdPaySchDApiService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.condition.CstBankQueryCondition;
import fl.customer.facade.customer.dto.CstBankDTO;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.condition.SplBankQueryCondition;
import fl.spl.facade.spl.dto.SplBankDTO;

/**
 * @Description: 付款收款方信息表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapPayeeInfoServiceImpl implements ICapPayeeInfoService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapPayeeInfoServiceImpl.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private ICapPayeeInfoApiService capPayeeInfoApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISplInfoApiService splInfoApiService;
	@Autowired
	private ICustomerQueryApiService customerQueryApiService;
	@Autowired
	private IPrjPrdPaySchDApiService prjPrdPaySchDApiService;
	@Autowired
	private IPrjPrdDtlService prjPrdDtlService;
	@Autowired
	private IPrjLnkCompBkAccService prjLnkCompBkAccService;
	@Autowired
	private ISaleRebAccInfoApiService saleRebAccInfoApiService;

	@Override
	public CapPayeeInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapPayeeInfoDTO dto = this.capPayeeInfoApiService.selectCapPayeeInfoById(id);
		return CapPayeeInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapPayeeInfoVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capPayeeInfoApiService.addCapPayeeInfo(CapPayeeInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capPayeeInfoApiService.removeCapPayeeInfoByIds(ids, CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapPayeeInfoVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capPayeeInfoApiService.modifyCapPayeeInfo(CapPayeeInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapPayeeInfoQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapPayeeInfoQueryCondition());
        }
		condition=(CapPayeeInfoQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capPayeeInfoApiService.searchCapPayeeInfoListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapPayeeInfoVO> voList=CapPayeeInfoVOTransform.toVOList((List<CapPayeeInfoDTO>)page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapPayeeInfoVO> searchList(CapPayeeInfoQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapPayeeInfoQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapPayeeInfoVO> voList=CapPayeeInfoVOTransform.toVOList(this.capPayeeInfoApiService.searchCapPayeeInfoList(condition));
		this.setDictData(voList);
		return voList;
	}
	
	@Override
	public List<CapPayeeInfoVO> searchListByPrjPrd(CapPayeeInfoQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapPayeeInfoQueryCondition();
		List<CapPayeeInfoVO> voList=CapPayeeInfoVOTransform.toVOList(this.capPayeeInfoApiService.searchListByPrjPrd(condition));
		this.setDictData(voList);
		return voList;
	}

    /**
     * 查询收款方
     * @param condition
     * @return
     * @throws BaseException
     */
    @Override
    public List<CapPayeeInfoVO> getPayeeList(CapPayeeInfoQueryCondition condition) throws BaseException{
        BaseLogUtils.info(logger,"getPayeeList","查询收款方===>"+condition.getPayeeTypCd());
        //根据收款方类型查询收款方信息
        List<CapPayeeInfoVO> list=new ArrayList<>();
        CapPayeeInfoVO payee=null;
        Map<String,String> bkAccTypCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), CapitalConstant.CODE_SPL_BK_ACC_TYP_CD);//账号类型：对公对私
        if(CapPayeeInfoConstant.CODE_PAYEE_TYP_CD_SPL.equals(condition.getPayeeTypCd())){//供应商
            SplBankQueryCondition qc=new SplBankQueryCondition();
            //查询供应商银行账户信息
            qc.setSplId(condition.getSplId());
            //审核状态通过的
            qc.setAudStsCd(CapitalConstant.CODE_SPL_BK_AUD_STS_CD_2);
            if(StringUtils.isNotBlank(condition.getPurpCd())){
                qc.setPurpCd(condition.getPurpCd());//账户用途
            }
            List<SplBankDTO> bkList=splInfoApiService.searchPrdSplBkList(qc,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
            if(CollectionUtils.isEmpty(bkList)){
                BaseLogUtils.info(logger,"getPayeeList","查询供应商审核通过的收款账户===>返回空");
                return list;
            }
            BaseLogUtils.info(logger,"getPayeeList","查询供应商审核通过的收款账户===>"+bkList.size());
            //账户用途
            Map<String,String> purpCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.PURP_CD);

            for(SplBankDTO dto:bkList){
                payee=new CapPayeeInfoVO();
                payee.setBkId(dto.getId());
                payee.setAccNo(dto.getAccNo());//收款账号
                payee.setRcptUnt(dto.getRcptUnt());//收款单位/人
				payee.setBkAccTypCd(dto.getBizTypCd());//对公/对私
				payee.setPayeeBkNo(dto.getBkNo());//收款方银行开户行号
                payee.setPayeeBkNm(dto.getBkNm());//开户银行名称
                if(bkAccTypCdMap!=null&&bkAccTypCdMap.containsKey(dto.getBizTypCd())){
                    payee.setBkAccTypCdNm(bkAccTypCdMap.get(dto.getBizTypCd()));
                }
                payee.setPurpCd(dto.getPurpCd());
                if(CollectionUtils.isMapNotEmpty(purpCdMap)&&purpCdMap.containsKey(dto.getPurpCd())){
                    payee.setPurpCdNm(purpCdMap.get(dto.getPurpCd()));
                }
                list.add(payee);
            }
        }else if(CapitalConstant.CODE_PAYEE_TYP_CD_8.equals(condition.getPayeeTypCd()) || CapPayeeInfoConstant.CODE_PAYEE_TYP_CD_CST.equals(condition.getPayeeTypCd())){//承租人或者担保人
            //查询客户银行账户信息
            List<Long> cstIdList=new ArrayList<>();
            cstIdList.add(condition.getCstId());
            CstBankQueryCondition bankQueryCondition=new CstBankQueryCondition();
            bankQueryCondition.setCstIdList(cstIdList);
            //验证状态：1.验证通过  查询验证通过的银行账户信息   CapitalConstant.CODE_CST_BK_AUD_STS_CD_1
            List<CstBankDTO> cstBankList=customerQueryApiService.searchCstBankWithCstInfoList(bankQueryCondition);
            if(CollectionUtils.isEmpty(cstBankList)){
                BaseLogUtils.info(logger,"getPayeeList","查询承租人/担保人收款账户===>返回空");
                return list;
            }
            BaseLogUtils.info(logger,"getPayeeList","查询承租人/担保人收款账户===>"+cstBankList.size());

            //收款账号类型映射
            List<SysDictionaryDTO> accTypCdList=this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),CapitalConstant.CODE_CST_BK_ACC_TYP_CD);
            Map<String,String> accTypCdMap=null;
            if(CollectionUtils.isNotEmpty(accTypCdList)){
                accTypCdMap=new HashMap<>(accTypCdList.size());
                for(SysDictionaryDTO dict:accTypCdList){
                    accTypCdMap.put(dict.getCode(),dict.getCodeAttr2());
                }
            }

            for(CstBankDTO dto:cstBankList){
                payee=new CapPayeeInfoVO();
                payee.setBkId(dto.getId());//
                payee.setAccNo(dto.getAccNo());//收款账号
                payee.setRcptUnt(dto.getAccNm());//收款单位/人
                if(accTypCdMap!=null&&accTypCdMap.containsKey(dto.getAccTypCd())){
                    payee.setBkAccTypCd(accTypCdMap.get(dto.getAccTypCd()));//收款账号类型 对公对私
                    if(bkAccTypCdMap!=null&&bkAccTypCdMap.containsKey(payee.getBkAccTypCd())){
                        payee.setBkAccTypCdNm(bkAccTypCdMap.get(payee.getBkAccTypCd()));
                    }
                }
                payee.setPayeeBkNo(dto.getBkNo());//收款方银行开户行号  开户行号为空有没有问题？？？
                if(StringUtils.isNotEmpty(dto.getBkCd())){
                    SysDictionaryDTO dict=this.sysDictionaryApiService.getCode(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),CapitalConstant.CODE_CAP_BK_CD,dto.getBkCd());
                    if(dict!=null){
                        payee.setPayeeBkNm(dict.getCodeNm());//开户银行名称
                    }
                }else{
                    continue;
                }
                list.add(payee);
            }
        }else if(CapitalConstant.CODE_PAYEE_TYP_CD_4.equals(condition.getPayeeTypCd())){
            //省公司

        }else if(CapitalConstant.CODE_PAYEE_TYP_CD_5.equals(condition.getPayeeTypCd())){
            //客户经理

        }else if(CapitalConstant.CODE_PAYEE_TYP_CD_6.equals(condition.getPayeeTypCd())){
            //车立行
            //查询挂靠公司ID
            PrjPrdDtlVO prjPrdDtlVO = this.prjPrdDtlService.selectById(condition.getPrjPrdDtlId());
            if(prjPrdDtlVO == null){
                BaseLogUtils.info(logger, "getPayeeList", "查询收款方银行账户信息出错：项目产品明细为空="+condition.getPrjPrdDtlId(), CurrentThreadContext.getCurrentUserName());
            }
            if(StringUtils.isEmpty(prjPrdDtlVO.getLnkCompId())){
                BaseLogUtils.info(logger, "getPayeeList", "查询收款方银行账户信息出错：车立行挂靠公司ID为空="+condition.getPrjPrdDtlId(), CurrentThreadContext.getCurrentUserName());
            }else{
                PrjLnkCompBkAccQC qc = new PrjLnkCompBkAccQC();
                qc.setLnkCompId(prjPrdDtlVO.getLnkCompId());
                List<String> audStsCdList = new ArrayList<>(1);
                audStsCdList.add(CapitalConstant.CODE_SPL_BK_AUD_STS_CD_2);
                qc.setAudStsCdList(audStsCdList);
                if(CapitalConstant.CODE_SPL_BK_PURP_CD_5.equals(condition.getPurpCd())){
                    //车立行购置税
                    qc.setPurpCd(CapitalConstant.CODE_SPL_BK_PURP_CD_2);
                }else{
                    //车立行收款账号
                    qc.setPurpCd(CapitalConstant.CODE_SPL_BK_PURP_CD_1);
                }

                //账号用途:1收款账户 2购置税账户
                Map<String, String> purpCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),
                        CurrentThreadContext.getCurrentOrgCd(),DictionaryConstant.COMP_PURP_CD);
                List<PrjLnkCompBkAccVO> prjLnkCompBkAccList = this.prjLnkCompBkAccService.getPrjLnkCompBkAccCondition(qc);
                for(PrjLnkCompBkAccVO bkAccVO : prjLnkCompBkAccList){
                    payee = new CapPayeeInfoVO();
                    payee.setBkId(bkAccVO.getId());
                    payee.setAccNo(bkAccVO.getAccNo());//收款账号
                    payee.setRcptUnt(bkAccVO.getRcptUnt());//收款单位/人
                    payee.setBkAccTypCd(bkAccVO.getBizTypCd());//对公/对私
                    payee.setPayeeBkNo(bkAccVO.getBkNo());//收款方银行开户行号
                    payee.setPayeeBkNm(bkAccVO.getBkNm());//开户银行名称
                    if(bkAccTypCdMap!=null&&bkAccTypCdMap.containsKey(bkAccVO.getBizTypCd())){
                        payee.setBkAccTypCdNm(bkAccTypCdMap.get(bkAccVO.getBizTypCd()));
                    }
                    payee.setPurpCd(condition.getPurpCd());
                    if(CollectionUtils.isMapNotEmpty(purpCdMap)&&purpCdMap.containsKey(condition.getPurpCd())){
                        payee.setPurpCdNm(purpCdMap.get(condition.getPurpCd()));
                    }
                    list.add(payee);
                }
            }
        }else if(CapitalConstant.CODE_PAYEE_TYP_CD_7.equals(condition.getPayeeTypCd())){
            Map<String,String> purpCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.PURP_CD);
            //查询机构销售返利账号
            SaleRebAccInfoQC saleRebAccInfoQC = new SaleRebAccInfoQC();
            saleRebAccInfoQC.setAudStsCd("2");
            saleRebAccInfoQC.setOrgCd(condition.getOrgCd());
            List<SaleRebAccInfoDTO> rebAccInfoDTOList = this.saleRebAccInfoApiService.searchList(saleRebAccInfoQC);
            for (SaleRebAccInfoDTO saleRebAccInfoDTO : rebAccInfoDTOList) {
                payee = new CapPayeeInfoVO();
                payee.setBkId(saleRebAccInfoDTO.getId());
                payee.setAccNo(saleRebAccInfoDTO.getAccNo());//收款账号
                payee.setRcptUnt(saleRebAccInfoDTO.getRcptUnt());//收款单位/人
                payee.setBkAccTypCd(saleRebAccInfoDTO.getBizTypCd());//对公/对私
                payee.setPayeeBkNo(saleRebAccInfoDTO.getBkNo());//收款方银行开户行号
                payee.setPayeeBkNm(saleRebAccInfoDTO.getBkNm());//开户银行名称
                if(bkAccTypCdMap!=null&&bkAccTypCdMap.containsKey(saleRebAccInfoDTO.getBizTypCd())){
                    payee.setBkAccTypCdNm(bkAccTypCdMap.get(saleRebAccInfoDTO.getBizTypCd()));
                }
                payee.setPurpCd(condition.getPurpCd());

                if(CollectionUtils.isMapNotEmpty(purpCdMap)&&purpCdMap.containsKey(condition.getPurpCd())){
                    payee.setPurpCdNm(purpCdMap.get(condition.getPurpCd()));
                }
                list.add(payee);
            }
        }else{
            BaseLogUtils.info(logger, "getPayeeList", "查询收款方银行账户信息出错：收款方类型无效="+condition.getPayeeTypCd(), CurrentThreadContext.getCurrentUserName());
        }
        return list;
    }

    /**
     * 查询事业部对应收款账户类型（用途）
     *
     * @param orgCd
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> getPayeeByOrgCd(String orgCd) throws BaseException {
        List<SysDictionaryDTO> dictList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),orgCd,CapitalConstant.CODE_PAYEE_ACC_PURP_CD);
        if(CollectionUtils.isNotEmpty(dictList)&&dictList.size()>1){
            Collections.sort(dictList,comparator);
        }

        List<KeyValueVO> list = new ArrayList<>();
        KeyValueVO kv = null;
        for(SysDictionaryDTO dict:dictList){
            kv = new KeyValueVO();
            kv.setKey(dict.getCode());
            kv.setValue(dict.getCodeNm());
            list.add(kv);
        }
        return list;
    }

    /**
     * 查询事业部对应收款账户类型（用途）
     *
     * @param orgCd
     * @param rebateMap
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> getPayeeByOrgCd(String orgCd,Long paySchId,Map<String, Long> rebateMap,String prjTypCd) throws BaseException {
        if(null == rebateMap){
            rebateMap = new HashMap<>(16);
        }
        List<SysDictionaryDTO> dictList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),orgCd,CapitalConstant.CODE_PAYEE_ACC_PURP_CD);
        if(CollectionUtils.isNotEmpty(dictList)&&dictList.size()>1){
            Collections.sort(dictList,comparator);
        }

        List<KeyValueVO> list = new ArrayList<>();
        KeyValueVO kv = null;
        //费用类型List
        List<String> feeTypCdList = new ArrayList<>();
        Map<String,String> feePurpMap = new HashMap<>(16);
        for(SysDictionaryDTO dict:dictList){
            if(StringUtils.isNotBlank(dict.getCodeAttr2()) && StringUtils.isNotBlank(prjTypCd) && !dict.getCodeAttr2().contains(prjTypCd)){
                continue;
            }
            kv = new KeyValueVO();
            kv.setKey(dict.getCode());
            kv.setValue(dict.getCodeNm());
            list.add(kv);
            if(StringUtils.isNotBlank(dict.getCodeAttr1())){
                if(dict.getCodeAttr1().contains(";")){
                    String[] feeTypCdArray = dict.getCodeAttr1().split(";");
                     for(String feeTypCd : feeTypCdArray){
                         feeTypCdList.add(feeTypCd);
                         feePurpMap.put(feeTypCd,dict.getCode());
                     }
                     continue;
                }
                feeTypCdList.add(dict.getCodeAttr1());
                //rebateMap.put(dict.getCode(), 0L);
                feePurpMap.put(dict.getCodeAttr1(),dict.getCode());
            }
        }

        //查询费用类型对应的支付表明细
        if(CollectionUtils.isNotEmpty(feeTypCdList)){
            PrjPrdPaySchDQueryCondition paySchDQueryCondition = new PrjPrdPaySchDQueryCondition();
            paySchDQueryCondition.setPaySchId(paySchId);
            paySchDQueryCondition.setPayPd(0L);
            paySchDQueryCondition.setIncludeFeeTypCdList(feeTypCdList);
            List<PrjPrdPaySchDDTO> paySchDDTOList = this.prjPrdPaySchDApiService.searchPrjPrdPaySchDList(paySchDQueryCondition);
            if(CollectionUtils.isNotEmpty(paySchDDTOList)){
                //Map<String,Long> feeMap = new HashMap<>(paySchDDTOList.size());
                for(PrjPrdPaySchDDTO paySchDDTO : paySchDDTOList){
                    if(paySchDDTO.getFeeAmt()>0d){
                        if(feePurpMap.containsKey(paySchDDTO.getFeeTypCd())){
                            //feeMap.put(feePurpMap.get(paySchDDTO.getFeeTypCd()),paySchDDTO.getFeeAmt().longValue());
                            if(rebateMap.containsKey(feePurpMap.get(paySchDDTO.getFeeTypCd()))){
                                rebateMap.put(feePurpMap.get(paySchDDTO.getFeeTypCd()),
                                        paySchDDTO.getFeeAmt().longValue()+rebateMap.get(feePurpMap.get(paySchDDTO.getFeeTypCd())));
                            }else{
                                rebateMap.put(feePurpMap.get(paySchDDTO.getFeeTypCd()),paySchDDTO.getFeeAmt().longValue());
                            }
                        }
                    }
                }

                /*for(Map.Entry<String,Long> entry : rebateMap.entrySet()){
                    if(feeMap.containsKey(entry.getKey())){
                        entry.setValue(feeMap.get(entry.getKey()));
                    }
                }*/

                feePurpMap.clear();
                feePurpMap = null;
                //feeMap.clear();
                //feeMap = null;
            }
        }


        //特殊处理
        if(PrjBscInfoConstant.CODE_BU_ORG_CD_ZK.equals(orgCd)){
            //如果存在工行返利就把工行返利当做供应商返利处理
            if(rebateMap.containsKey(CapitalConstant.CODE_SPL_BK_PURP_CD_9)&&rebateMap.get(CapitalConstant.CODE_SPL_BK_PURP_CD_9)>0L){
                rebateMap.put(CapitalConstant.CODE_SPL_BK_PURP_CD_2,rebateMap.get(CapitalConstant.CODE_SPL_BK_PURP_CD_9));
                rebateMap.remove(CapitalConstant.CODE_SPL_BK_PURP_CD_9);
            }
        }

        return list;
    }

    Comparator<SysDictionaryDTO> comparator = new Comparator<SysDictionaryDTO>() {
        public int compare(SysDictionaryDTO d1, SysDictionaryDTO d2) {
            return d1.getSortNo() - d2.getSortNo();
        }
    };

    /**
     * 查询支付表返利金额
     *
     * @param paySchId
     * @return
     * @throws BaseException
     */
    @Override
    public List<PrjPrdPaySchDDTO> searchPrjRebate(Long paySchId) throws BaseException {
        PrjPrdPaySchDQueryCondition paySchDQueryCondition = new PrjPrdPaySchDQueryCondition();
        paySchDQueryCondition.setPaySchId(paySchId);
        paySchDQueryCondition.setPayPd(0L);
        List<String> feeTypCdList = new ArrayList<>(2);
        feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_08);//供应商返利
        feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_37);//GPS返利
        paySchDQueryCondition.setIncludeFeeTypCdList(feeTypCdList);
        return this.prjPrdPaySchDApiService.searchPrjPrdPaySchDList(paySchDQueryCondition);
    }

    /**
     * 查询支付表返利金额
     *
     * @param paySchId
     * @return
     * @throws BaseException
     */
    @Override
    public List<PrjPrdPaySchDDTO> searchPrjRebateForSpecialCar(Long paySchId) throws BaseException {
        PrjPrdPaySchDQueryCondition paySchDQueryCondition = new PrjPrdPaySchDQueryCondition();
        paySchDQueryCondition.setPaySchId(paySchId);
        paySchDQueryCondition.setPayPd(0L);
        List<String> feeTypCdList = new ArrayList<>(4);
        feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_08);//供应商返利
        //feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_30);//购置税
        //feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_32);//保险总额
        //feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_24);//上牌抵押费
        paySchDQueryCondition.setIncludeFeeTypCdList(feeTypCdList);
        return this.prjPrdPaySchDApiService.searchPrjPrdPaySchDList(paySchDQueryCondition);
    }

    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapPayeeInfoVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		//收款方类型
		Map<String, String> payeeTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapPayInfoConstant.CODE_PAYEE_TYP_CD);
		//款项
		//Map<String, String> fundTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapPayInfoConstant.CODE_FUND_TYP_CD);
		//付款方式
		Map<String, String> payWayCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapPayInfoConstant.CODE_PAY_WAYP_CD);
		//账号用途
        Map<String,String> purpCdMap=this.sysDictionaryApiService.searchGroupToMapAll(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),DictionaryConstant.PURP_CD);

		for(CapPayeeInfoVO vo:voList){
			if (payeeTypCdMap != null && payeeTypCdMap.containsKey(vo.getPayeeTypCd())) {
            	vo.setPayeeTypCdNm(payeeTypCdMap.get(vo.getPayeeTypCd()));
        	}
			/*if (fundTypCdMap != null && fundTypCdMap.containsKey(vo.getFundTypCd())) {
				vo.setFundTypCdNm(fundTypCdMap.get(vo.getFundTypCd()));
			}*/
			if(payWayCdMap!=null && payWayCdMap.containsKey(vo.getPayWayCd())){
				vo.setPayWayCdNm(payWayCdMap.get(vo.getPayWayCd()));
			}
			if(purpCdMap!=null && purpCdMap.containsKey(vo.getPurpCd())){
			    vo.setPurpCdNm(purpCdMap.get(vo.getPurpCd()));
            }
		}
	}

	@Override
	public boolean batchModify(List<CapPayeeInfoVO> VOList)
			throws BaseException {
		if(CollectionUtils.isEmpty(VOList)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.capPayeeInfoApiService.batchModifyCapPayeeInfo(CapPayeeInfoVOTransform.toDTOList(VOList));
	}

    @Override
    public List<CapPayeeInfoVO> searchListForSpecialCar(CapPayeeInfoQueryCondition condition) throws BaseException{
        if(null == condition) condition=new CapPayeeInfoQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        List<CapPayeeInfoVO> voList=CapPayeeInfoVOTransform.toVOList(this.capPayeeInfoApiService.searchCapPayeeInfoList(condition));
        this.setDictDataForSpecialCar(voList);
        return voList;
    }

    /**
     * 专车添加数据字典名称
     * @param voList
     */
    public void setDictDataForSpecialCar(List<CapPayeeInfoVO> voList){
        if(CollectionUtils.isEmpty(voList)) return;
        //收款方类型
        Map<String, String> payeeTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapPayInfoConstant.CODE_PAYEE_TYP_CD);
        //款项
        //Map<String, String> fundTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapPayInfoConstant.CODE_FUND_TYP_CD);
        //付款方式
        Map<String, String> payWayCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapPayInfoConstant.CODE_PAY_WAYP_CD);
        //账号用途
//        Map<String,String> purpCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.PURP_CD);
        //查询数据字典 账号用途
        List<KeyValueVO> kvList = this.getPayeeByOrgCd("00000008");
        Map<String, String> purpCdMap = new HashMap<>();
        for (KeyValueVO kv : kvList) {
            purpCdMap.put(kv.getKey(),kv.getValue());
        }

        for(CapPayeeInfoVO vo:voList){
            if (payeeTypCdMap != null && payeeTypCdMap.containsKey(vo.getPayeeTypCd())) {
                vo.setPayeeTypCdNm(payeeTypCdMap.get(vo.getPayeeTypCd()));
            }
			/*if (fundTypCdMap != null && fundTypCdMap.containsKey(vo.getFundTypCd())) {
				vo.setFundTypCdNm(fundTypCdMap.get(vo.getFundTypCd()));
			}*/
            if(payWayCdMap!=null && payWayCdMap.containsKey(vo.getPayWayCd())){
                vo.setPayWayCdNm(payWayCdMap.get(vo.getPayWayCd()));
            }
            if(purpCdMap!=null && purpCdMap.containsKey(vo.getPurpCd())){
                vo.setPurpCdNm(purpCdMap.get(vo.getPurpCd()));
            }
        }
    }

    /**
     * 查询保险保养账户金额
     *
     * @param paySchId
     * @return
     * @throws BaseException
     */
    @Override
    public List<PrjPrdPaySchDDTO> searchPrjSplBkPurpAmt(Long paySchId) throws BaseException {
        PrjPrdPaySchDQueryCondition paySchDQueryCondition = new PrjPrdPaySchDQueryCondition();
        paySchDQueryCondition.setPaySchId(paySchId);
        paySchDQueryCondition.setPayPd(0L);
        List<String> feeTypCdList = new ArrayList<>(2);
        feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_32);//保险总额
        feeTypCdList.add(CapitalConstant.CODE_FEE_TYP_CD_35);//保养
        paySchDQueryCondition.setIncludeFeeTypCdList(feeTypCdList);
        return this.prjPrdPaySchDApiService.searchPrjPrdPaySchDList(paySchDQueryCondition);
    }

}
